package com.example.tellwin.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.example.tellwin.MainApplication;
import com.example.tellwin.utils.ToastUtil;

import static android.content.Context.WINDOW_SERVICE;

/**
 * description
 *
 * @author created by wuwang on 2016/4/25
 */
public abstract class BaseFragment extends Fragment {

    public View rootView;
    protected Context mContext;
    protected Handler handler = new Handler();

    protected void findViews(View layout) {
    }

    protected void initialize() {
    }

    protected void bindEvent() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mContext = getActivity().getApplicationContext();
//        return super.onCreateView(inflater, container, savedInstanceState);
        if (rootView == null) {
            rootView = onCreateRootView(inflater, container);
        } else {
            if (rootView.getParent() != null) {
                ((ViewGroup) rootView.getParent()).removeView(rootView);
            }
        }
        return rootView;
    }

    public abstract View onCreateRootView(LayoutInflater inflater, ViewGroup container);

    public View findViewById(int id) {
        return rootView.findViewById(id);
    }

    public int gColor(int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return getActivity().getColor(id);
        } else {
            return this.getResources().getColor(id);
        }
    }

    protected void hideSoftKeyboard() {
        View view = getActivity().getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public boolean onBackPressed() {
        return true;
    }

    public void toast(CharSequence toast) {
//        @SuppressLint("ShowToast") Toast t= Toast.makeText(AppUtils.getAppContext(), toast, Toast.LENGTH_SHORT);
//        t.show();
//        return t;
        ToastUtil.show(MainApplication.getContext(),toast);
    }

    public BaseActivity getBaseActivity() {
        return (BaseActivity) getActivity();
    }


    public int getDisplayWidth() {
        int width = ((WindowManager) getActivity().getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        return width;
    }

    public int getDisplayHeight() {
        int height = ((WindowManager) getActivity().getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getHeight();
        return height;
    }

    protected void openActivity(Class<?> cls) {
        if (isAdded()) {
            Intent i = new Intent(mContext, cls);
            startActivity(i);
        }
    }

    protected void openActivity(Class<?> cls, int tag) {
        if (isAdded()) {
            Intent i = new Intent(mContext, cls);
            i.putExtra("tag", tag);
            startActivity(i);
        }
    }

    protected void openActivity(Class<?> cls, Bundle bundle) {
        if (bundle == null) {
            openActivity(cls);
            return;
        }
        Intent i = new Intent(mContext, cls);
        i.putExtras(bundle);
        startActivity(i);
//        int version = Integer.valueOf(Build.VERSION.SDK);
    }

    protected void openActivityForResult(Class<?> cls, int requestCode) {
        Intent i = new Intent(mContext, cls);
        startActivityForResult(i, requestCode);
        int version = Integer.valueOf(Build.VERSION.SDK);
    }
    protected void openActivityForResult(Class<?> cls) {
        Intent i = new Intent(mContext, cls);
        startActivityForResult(i, 1);
    }
    protected void openActivityForResult(Class<?> cls, int requestCode, Bundle bundle) {
        Intent i = new Intent(mContext, cls);
        i.putExtras(bundle);
        startActivityForResult(i, requestCode);
        int version = Integer.valueOf(Build.VERSION.SDK);
    }

}
